package com.springboot.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.springboot.feign.VirtualWalletFeign;
import com.springboot.feign.VirtualWalletFeignFallBack;
import com.springboot.model.bo.VirtualWalletBO;
import com.springboot.model.bo.VirtualWalletTransactionBO;
import com.springboot.model.po.VirtualWalletPO;
import com.springboot.model.po.VirtualWalletTransactionPO;
import com.springboot.model.vo.VirtualWalletTransactionVO;
import com.springboot.model.vo.VirtualWalletVO;
import com.springboot.service.VirtualWalletService;
import com.springboot.service.VirtualWalletTransactionService;

import util.VerifyUtil;

@RefreshScope	//监听动态刷新
@RestController
@RequestMapping("/virtualWalletTransactions")
public class VirtualWalletTransactionController {
	@Autowired
	private VirtualWalletTransactionService transactionService;
//	@Autowired
//	private DiscoveryClient discoveryClient;
//	@Autowired
//	private RestTemplate restTemplate;
//	@Bean
//	public RestTemplate restTemplate() {
//		return new RestTemplate();
//	}
	
	@Autowired
	private VirtualWalletFeign virtualWalletFeign;
	
	@GetMapping("/{walletId}")
    public ResponseEntity listTransactionBywalletId(@PathVariable Integer walletId , @RequestParam String token){
		ResponseEntity<VirtualWalletVO> result_ = virtualWalletFeign.getVirtualWallet(walletId, token);
		// 熔断处理
		if(result_.getStatusCode().equals(HttpStatus.FORBIDDEN))
			return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ArrayList());
		String userId_target = VerifyUtil.verify(token);
		// 信息权限处理
		if(userId_target == null || !result_.getBody().getUserId().equals(userId_target)) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Unauthorized");//401
		}
		List<VirtualWalletTransactionBO> list = transactionService.listTransactionBywalletId(walletId);
		// 资源不存在处理
		if(list.size() == 0 || list == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("The requested resource was not found");
		}else {
			List result = new ArrayList<VirtualWalletTransactionVO>();
			for(VirtualWalletTransactionBO vbo : list) {
				VirtualWalletTransactionVO vvo = new VirtualWalletTransactionVO();
				vvo.setAmount(vbo.getAmount());
				vvo.setCreateTime(vbo.getCreateTime());
				vvo.setFromWalletId(vbo.getFromWalletId());
				vvo.setToWalletId(vbo.getToWalletId());
				vvo.setStatus(vbo.getStatus());
				vvo.setTransactionId(vbo.getTransactionId());
				vvo.setWalletId(vbo.getWalletId());
	    		result.add(vvo);
	    	}
			return ResponseEntity.ok(result);
		}
	}

}
